

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

class Solution
{
public:
	bool isHappy(int n)
	{
		if (1 == n)
			return true;

		int slow = _Next(n), fast = _Next(slow);

		while (slow != fast)
		{
			slow = _Next(slow);
			fast = _Next(_Next(fast));
		}

		return 1 == fast;
	}

	int numJewelsInStones(string jewels, string stones)
	{
		int res = 0;
		vector<int> cnt(58);

		for (const auto& e : jewels)
		{
			++cnt[e - 'A'];
		}
		for (const auto& e : stones)
		{
			if (cnt[e - 'A'])
				++res;
		}

		return res;
	}

private:
	int _Next(int n)
	{
		int res = 0;

		for (int x = 0; n; n /= 10)
		{
			x = n % 10;
			res += x * x;
		}

		return res;
	}
};

int GetNext(int n)
{
	int res = 0;

	while (n)
	{
		res += (n % 10) * (n % 10);
		n /= 10;
	}

	return res;
}

void Test1()
{
	int n1 = INT_MAX;
	const int times1 = 10;
	const int times2 = 33;

	for (int j = 0, tmp = 0; j < times1; ++j)
	{
		tmp = n1;

		cout << n1 << " : ";
		for (int i = 0; i < times2; ++i)
		{
			n1 = GetNext(n1);
			printf("%d ", n1);
		}
		cout << endl;

		n1 = tmp / 10;
	}

	int n2 = 19;
	const int times3 = 10;

	cout << n2 << " : ";
	for (int i = 0; i < times3; ++i)
	{
		n2 = GetNext(n2);
		printf("%d ", n2);
	}
	cout << endl;
}

int main()
{
	Test1();


	return 0;
}